package com.example.villagedevelop.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.villagedevelop.generator.entity.*;
import com.example.villagedevelop.generator.mapper.HomeStayMapper;
import com.example.villagedevelop.generator.mapper.ImageHomeStayMapper;
import com.example.villagedevelop.generator.service.HomeStayService;
import com.example.villagedevelop.generator.service.ImageHomeStayService;
import com.example.villagedevelop.generator.service.VideoHomeStayService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class HomeStayServiceImpl extends ServiceImpl<HomeStayMapper, HomeStay> implements HomeStayService {

    @Autowired
    ImageHomeStayService imageHomeStayService;
    @Autowired
    HomeStayMapper mapper;
    @Autowired
    ImageHomeStayMapper imageHomeStayMapper;
    @Autowired
    HomeStayService homeStayService;
    @Autowired
    VideoHomeStayService videoHomeStayService;

    //按默认排序返回所有的数据
    @Override
    public List<HomeStayVO> findAll() {
        List<HomeStayVO> homeStayVOList = new ArrayList<>();
        QueryWrapper<HomeStay> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("default_sort");
        List<HomeStay> homeStayList = mapper.selectList(wrapper);

        return this.homeListtoVOList(homeStayList);
    }

    //把HomeStay的list转换成HomeStay的list，即附加上image
    @Override
    public List<HomeStayVO> homeListtoVOList(List<HomeStay> homeStayList) {
        List<HomeStayVO> homeStayVOList = new ArrayList<>();

        for (HomeStay homeStay : homeStayList){
            HomeStayVO homeStayVO = new HomeStayVO();
            BeanUtils.copyProperties(homeStay,homeStayVO);

            int sid = homeStay.getId();
            homeStayVO.setImages(imageHomeStayService.getImagesBySid(sid));
            homeStayVO.setVideoes(videoHomeStayService.getVideoesBySid(sid));
            homeStayVOList.add(homeStayVO);
        }
        return homeStayVOList;
    }

    //通过town查询
    @Override
    public List<HomeStayVO> findHomeStayVOByTown(String town) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("town",town);
        List<HomeStay> homeStayList = mapper.selectList(wrapper);

        return this.homeListtoVOList(homeStayList);
    }

    //根据town查找其关联的下层village
    @Override
    public List<String> findVillage(String town) {
        List<HomeStay> homeStayList = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("town",town);
        homeStayList = mapper.selectList(wrapper);

        List<String> villages = new ArrayList<>();
        for (HomeStay homeStay:homeStayList){
            villages.add(homeStay.getVillage());
        }
        //去重
        LinkedHashSet<String> hashSet = new LinkedHashSet<>(villages);
        ArrayList<String> villageList = new ArrayList<>(hashSet);
        return villageList;

    }

    //通过town和village查找
    @Override
    public List<HomeStayVO> findHomeStayVOByTown_village(String town, String village) {
        QueryWrapper wrapper = new QueryWrapper();
        Map<String,String> map = new HashMap<>();
        map.put("town",town);
        map.put("village",village);
        wrapper.allEq(map);
        List<HomeStay> homeStayList = mapper.selectList(wrapper);

        return this.homeListtoVOList(homeStayList);
    }

    //范围筛查查找
    @Override
    public List<HomeStayVO> findHomeStayVO_Classify(String field, int min, int max) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.between(field,min,max);
        List<HomeStay> homeStayList = mapper.selectList(wrapper);

        return this.homeListtoVOList(homeStayList);
    }

    //删除数据
    @Override
    public void deleteObject(int id) {
        QueryWrapper<HomeStay> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        mapper.delete(wrapper);

        QueryWrapper<ImageHomeStay> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("s_id",id);
        imageHomeStayMapper.delete(wrapper1);
    }


    //改变默认排序
    @Override
    public void changeDefaultSort(int id1, int id2) {
        int sid1,sid2;

        QueryWrapper<HomeStay> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id1);
        HomeStay homeStay1 = homeStayService.getOne(wrapper);
        sid1 = homeStay1.getDefaultSort();
        wrapper = new QueryWrapper<>();
        wrapper.eq("id",id2);
        HomeStay homeStay2 = homeStayService.getOne(wrapper);
        sid2 = homeStay2.getDefaultSort();
        homeStay1.setDefaultSort(sid2);
        homeStay2.setDefaultSort(sid1);
        mapper.updateById(homeStay1);
        mapper.updateById(homeStay2);
    }


}
